<?php

	namespace app\Admin\controller\chat;

	use think\Controller;
	use think\Db;

	class Index extends Controller
	{

		public function save_message () {

			if ($this->request->isAjax()) {
				$data = [
					'fromid'   => intval($this->request->param('fromid')),
					'fromname' => $this->getMember(intval($this->request->param('fromid')))['nickname'],
					'toid'     => intval($this->request->param('toid')),
					'toname'   => $this->getMember(intval($this->request->param('toid')))['nickname'],
					'time'     => time(),
					'isread'   => intval($this->request->param('isread')),
					'type'     => 1,
					'content'  => htmlspecialchars($this->request->param('data'))

				];
				Db::name('chat_communication')->insert($data);
			}

		}


		public function getMember ($id) {
			return Db::name('admin')->field('id,nickname,avatar')->where('id', $id)->find();
		}

		public function getMemberAvatar () {
			if ($this->request->isAjax()) {
				$from_avatar = $this->request->domain().$this->getMember(intval($this->request->param('fromid')))['avatar'];
				$to_avatar = $this->request->domain().$this->getMember(intval($this->request->param('toid')))['avatar'];
				$this->returnSuccess(['from_avatar'=>$from_avatar,'to_avatar'=>$to_avatar]);
			}
		}

		public function getMemberName(){
			if($this->request->isAjax()){
				$this->returnSuccess($this->getMember(intval($this->request->param('toid'))));

			}
		}

		public function getMessageList(){
			$fromid = intval($this->request->param('fromid'));
			$toid = intval($this->request->param('toid'));
			$where = " ( fromid = $fromid AND toid = $toid ) || ( toid = $fromid AND fromid = $toid ) ";

			$count =  Db::name('chat_communication')->where($where)->count();
			$page = ceil($count/10);
			$list = Db::name('chat_communication')->where($where)->limit(( $page - 1 ) * 10,10)->order('id asc')->select();
			$this->returnSuccess($list);
		}


		#获取聊天列表,我是接受者
		public function getChitchatList(){

			if($this->request->isAjax()){
				$fromid = intval($this->request->param('fromid'));

				$count =  Db::name('chat_communication')->where(['toid'=>$fromid])->count();

				//获取给我发信息的人,即我是接受者
				$list =  Db::name('chat_communication')->where(['toid'=>$fromid])->group('fromid')->select();

				#这里没有使用foreach
				$rows = array_map(function ($val){
					return [
						'head_url' =>$this->request->domain().$this->getMember($val['fromid'])['avatar'],
						'username'=>$val['fromname'],
						'last_message'=>$this->getLastMessge($val['fromid'],$val['toid']),
						'countNoRead' =>$this->getCountNoRead($val['fromid'],$val['toid']),
						//现在身份的转变是,我给别人发。我就是查询出来的数据的接受者,即toid是我
						'chat_page'=>url('index/index/chatroom',array('fromid'=>$val['toid'],'toid'=>$val['fromid']))
					];
				},$list);

				$this->returnSuccess(['list'=>$rows]);
			}
		}

		#获取两者聊天的最后一条信息
		protected function getLastMessge($fromid,$toid){
			$where = " ( fromid = $fromid AND toid = $toid ) || ( toid = $fromid AND fromid = $toid ) ";
			return Db::name('chat_communication')->where($where)->order('id desc')->find()['content'];
		}

		protected function getCountNoRead($fromid,$toid){
			return Db::name('chat_communication')->where(['toid'=>$toid,'fromid'=>$fromid,'isread'=>0])->count();
		}
	}